---
import BaseLayout from "../../layouts/BaseLayout.astro";
import BlogPost from "../../components/BlogPost.astro";
import {getCollection} from "astro:content";
export async function getStaticPaths(): Promise<any> {
    const allPosts: any = await getCollection('posts');

    const uniqueTags = [...new Set(allPosts.map((post: any) => post.data.tags).flat())];
    return uniqueTags.map((tag: any) => {
        const filteredPosts = allPosts.filter((post: any) => post.data.tags.includes(tag));
        return {
            params: {tag},
            props: {posts: filteredPosts},
        };
    });
}

const {tag} = Astro.params;
const {posts} = Astro.props
---
<BaseLayout pageTitle={tag}>
    <p>包含 {tag} 标签的文章</p>
    <ul>
        {
            // @ts-ignore

            posts.map((post: any) =>
                    <BlogPost url={`/posts/${post.slug}`} title={post.data.title}/>)
        }
    </ul>
</BaseLayout>
